package com.imddysc.jzknavigator.controller;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.imddysc.jzknavigator.service.ZknavigatorService;
import com.imddysc.jzknavigator.utils.ZknaviUtils;

@Controller
public class ZknavigatorDeleteController {
	
	private static final Logger logger = LoggerFactory.getLogger(ZknavigatorDeleteController.class);
	
	@Autowired
	private ZknavigatorService zknavigatorService;
	
	
	@RequestMapping(value = "/delete/{path}")
	public ModelAndView delete(@PathVariable("path") String path) {
		ModelAndView mav = new ModelAndView();
		logger.info("delete: " + path);
		
		String path1 = path.replace("|","/");
		String path2 = path1.replace("//","/");
		logger.info("path: " + path);
		logger.info("path1: " + path1);
		logger.info("path2: " + path2);
		
		Map pahtMap = ZknaviUtils.pathHref(path);
		mav.addObject("pathAHrefList",pahtMap.get("pathAHrefList"));
		mav.addObject("pathUrl",pahtMap.get("pathUrl"));
		
		try {
			zknavigatorService.deletePathNode(path2);
			mav.addObject("message", "OK: deleted, 删除成功，请查看确认！");
		} catch (Exception e) {
			mav.addObject("message", "FAIL: deleted, 删除中异常，请查看确认是否删除！");
			e.printStackTrace();
		}
		
		
		
		mav.setViewName("delete");
		return mav;
	}

}
